package team.kirohuji.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import team.kirohuji.dao.PriorityDao;
import team.kirohuji.domain.Priority;
import team.kirohuji.service.PriorityService;
import team.kirohuji.service.PriorityService.MenuType;
import team.kirohuji.service.SystemUserService;

/**
 * 权限服务实现类
 * 
 * @author kirohuji
 *
 */
@Service("priorityService")
public class PriorityServiceImpl implements PriorityService {

	/**
	 * @see PriorityDao priorityDao
	 */
	@Resource
	private PriorityDao priorityDao;

	/**
	 * @see SystemUserService systemUserService
	 */
	@Resource
	private SystemUserService systemUserService;

	/*
	 * 查询权限列表通过父类的主键 (non-Javadoc)
	 * 
	 * @see
	 * team.kirohuji.service.PriorityService#queryListParentId(java.lang.Integer,
	 * java.lang.Integer, java.util.List)
	 * 
	 * @param parentId 父主键
	 * 
	 * @param priorityType 权限类型
	 * 
	 * @param menuIdList 菜单主键列表
	 * 
	 * @return Priority列表
	 */
	@Override
	public List<Priority> queryListParentId(Integer parentId, Integer priorityType, List<Integer> menuIdList) {

		List<Priority> menuList = priorityDao.queryListParentId(parentId, priorityType);
		if (menuIdList == null) {
			return menuList;
		}

		List<Priority> userMenuList = new ArrayList<>();
		for (Priority menu : menuList) {
			if (menuIdList.contains(menu.getPriorityId())) {
				userMenuList.add(menu);
			}
		}
		return userMenuList;
	}

	/*
	 * 查询不是按钮的权限列表 (non-Javadoc)
	 * 
	 * @see team.kirohuji.service.PriorityService#queryNotButtonList()
	 * 
	 * @return Priority列表
	 */
	@Override
	public List<Priority> queryNotButtonList() {

		return priorityDao.queryNotButtonList();
	}

	/*
	 * 获取用户菜单列表 (non-Javadoc)
	 * 
	 * @see team.kirohuji.service.PriorityService#getUserMenuList(java.lang.Integer,
	 * java.lang.Integer)
	 * 
	 * @param userId 用户主键
	 * 
	 * @param priorityType 权限类型
	 * 
	 * @return Priority列表
	 */
	@Override
	public List<Priority> getUserMenuList(Integer userId, Integer priorityType) {

		// 获取用户权限菜单
		List<Integer> menuIdList = systemUserService.queryAllMenuId(userId);
		return getAllMenuList(menuIdList, priorityType);
	}

	/*
	 * 查询对象通过菜单Id (non-Javadoc)
	 * 
	 * @see team.kirohuji.service.PriorityService#queryObject(java.lang.Integer)
	 * 
	 * @param menuId
	 * 
	 * @return Priority
	 */
	@Override
	public Priority queryObject(Integer menuId) {

		return priorityDao.queryObject(menuId);
	}

	/*
	 * 查询列表通过map对象 (non-Javadoc)
	 * 
	 * @see team.kirohuji.service.PriorityService#queryList(java.util.Map)
	 * 
	 * @param map对象
	 * 
	 * @return Priority对象
	 */
	@Override
	public List<Priority> queryList(Map<String, Object> map) {

		return priorityDao.queryList(map);
	}

	/*
	 * 获取总记录长度 (non-Javadoc)
	 * 
	 * @see team.kirohuji.service.PriorityService#queryTotal(java.util.Map)
	 * 
	 * @param map对象
	 * 
	 * @return int
	 */
	@Override
	public int queryTotal(Map<String, Object> map) {

		return priorityDao.queryTotal(map);
	}

	/*
	 * 存入一个Priority (non-Javadoc)
	 * 
	 * @see
	 * team.kirohuji.service.PriorityService#save(team.kirohuji.domain.Priority)
	 * 
	 * @param menu
	 */
	@Override
	public void save(Priority menu) {
		priorityDao.save(menu);
	}

	/*
	 * 更新 (non-Javadoc)
	 * 
	 * @see
	 * team.kirohuji.service.PriorityService#update(team.kirohuji.domain.Priority)
	 * 
	 * @param menu
	 */
	@Override
	public void update(Priority menu) {
		priorityDao.update(menu);
	}

	/*
	 * 批量删除 (non-Javadoc)
	 * 
	 * @see team.kirohuji.service.PriorityService#deleteBatch(java.lang.Integer[])
	 * 
	 * @param menuIds 菜单Id数组
	 */
	@Override
	@Transactional
	public void deleteBatch(Integer[] menuIds) {

		priorityDao.deleteBatch(menuIds);
	}

	/**
	 * 获取所有菜单列表
	 * 
	 * @param menuIdList
	 *            菜单Id列表
	 * @param priorityType
	 *            类型
	 * @return Priority列表
	 */
	private List<Priority> getAllMenuList(List<Integer> menuIdList, Integer priorityType) {
		// 查询根菜单列表
		List<Priority> menuList = queryListParentId(0, priorityType, menuIdList);
		// 递归获取子菜单
		getMenuTreeList(menuList, priorityType, menuIdList);

		return menuList;
	}

	/**
	 * 递归获取菜单树
	 * 
	 * @param menuList
	 *            菜单列表
	 * @param priorityType
	 *            类型
	 * @param menuIdList
	 *            菜单Id列表
	 * @return Priority列表
	 */
	private List<Priority> getMenuTreeList(List<Priority> menuList, Integer priorityType, List<Integer> menuIdList) {
		List<Priority> subMenuList = new ArrayList<Priority>();

		for (Priority entity : menuList) {
			if (entity.getNodeType() == MenuType.CATALOG.getValue()) {// 目录
				entity.setList(getMenuTreeList(queryListParentId(entity.getPriorityId(), priorityType, menuIdList),
						priorityType, menuIdList));
			}
			subMenuList.add(entity);
		}
		return subMenuList;
	}

}
